Introduction

Our goal is to add some interactivity to a “volcano plot”:

Data

Microarray data from a differential gene expression study (Chiaretti et al 2005)

diff_exp <- readRDS("widgets/diff_exp.rds")
head(diff_exp)
##      meanDiff       logp geneName
## 1  0.04345433 0.40724701  RABGGTA
## 2  0.04296986 0.34083644    MAPK3
## 3  0.03208350 0.18462685     TIE1
## 4 -0.01270016 0.08054529    CXCR5
## 5 -0.03177045 0.22418568    CXCR5
## 6 -0.42730253 1.08598298    DUSP1

Using plotly

Setup:

library("plotly")
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library("htmlwidgets")
p <- plot_ly(data = diff_exp, x = ~meanDiff, y = ~logp) %>%
    add_markers(
        text = diff_exp[["geneName"]],
        customdata = paste0("https://www.genecards.org/cgi-bin/carddisp.pl?gene=", diff_exp[["geneName"]]))
onRender(
    p, "
  function(el) {
    el.on('plotly_click', function(d) {
      var url = d.points[0].customdata;
      window.open(url);
    });
  }
")
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.

Using ggiraph

library("ggplot2")
library("ggiraph")
diff_exp$onclick <- sprintf("window.open(\"%s%s\")",
  "https://www.genecards.org/cgi-bin/carddisp.pl?gene=", diff_exp[["geneName"]])
p <- ggplot(diff_exp, 
            aes(meanDiff, logp)) +
    geom_point()
p

ip <- p + geom_point_interactive(
        aes(data_id = geneName, 
            tooltip = geneName,
            onclick = onclick))

girafe(ggobj = ip)

Session information

sessionInfo()
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Mojave 10.14.6
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/fr_FR.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] gdtools_0.2.2     ggiraph_0.7.8     htmlwidgets_1.5.1 plotly_4.9.2.1   
## [5] ggplot2_3.3.2    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.5        pillar_1.4.6      compiler_4.0.2    tools_4.0.2      
##  [5] uuid_0.1-4        digest_0.6.25     jsonlite_1.7.0    evaluate_0.14    
##  [9] lifecycle_0.2.0   tibble_3.0.3      gtable_0.3.0      viridisLite_0.3.0
## [13] pkgconfig_2.0.3   rlang_0.4.7       crosstalk_1.1.0.1 yaml_2.2.1       
## [17] xfun_0.16         withr_2.2.0       dplyr_1.0.2       stringr_1.4.0    
## [21] httr_1.4.2        knitr_1.29        systemfonts_0.2.3 generics_0.0.2   
## [25] vctrs_0.3.2       grid_4.0.2        tidyselect_1.1.0  glue_1.4.1       
## [29] data.table_1.13.0 R6_2.4.1          rmarkdown_2.3     farver_2.0.3     
## [33] purrr_0.3.4       tidyr_1.1.1       magrittr_1.5      scales_1.1.1     
## [37] ellipsis_0.3.1    htmltools_0.5.0   colorspace_1.4-1  labeling_0.3     
## [41] stringi_1.4.6     lazyeval_0.2.2    munsell_0.5.0     crayon_1.3.4